import request from '@/config/axios'
import { getRefreshToken } from '@/utils/auth'
import type { RegisterVO, UserLoginVO } from './types'

export interface SmsCodeVO {
  mobile: string
  scene: number
}

export interface SmsLoginVO {
  mobile: string
  code: string
}

/**
 * 管理后台 - 重置账号密码短信请求 VO
 */
export interface AuthResetPasswordReqVO {
  username: string // 登录账号
  captchaVerification: string // 滑动验证码
}

/**
 * 管理后台 - 重置账号密码短信请求 VO
 */
export interface AuthResetPasswordSaveReqVO {
  username: string // 登录账号
  password: string // 新密码
  code: string // 短信验证码
}

/**
 * 管理后台 - 2FA进行code登录
 */
export interface Auth2FALoginReqVO {
  code: string // 验证码
}

// 登录
export const login = (data: UserLoginVO) => {
  return request.post({ url: '/system/auth/login', data })
}

// 登录2FA
export const login2FA = (data: Auth2FALoginReqVO) => {
  return request.post({ url: '/system/auth/2fa-login', data })
}

// 注册
export const register = (data: RegisterVO) => {
  return request.post({ url: '/system/auth/register', data })
}

// 刷新访问令牌
export const refreshToken = () => {
  return request.post({ url: '/system/auth/refresh-token?refreshToken=' + getRefreshToken() })
}

// 使用租户名，获得租户编号
export const getTenantIdByName = (name: string) => {
  return request.get({ url: '/system/tenant/get-id-by-name?name=' + name })
}

// 使用租户域名，获得租户信息
export const getTenantByWebsite = (website: string) => {
  return request.get({ url: '/system/tenant/get-by-website?website=' + website })
}

// 登出
export const loginOut = () => {
  return request.post({ url: '/system/auth/logout' })
}

// 获取用户权限信息
export const getInfo = () => {
  return request.get({ url: '/system/auth/get-permission-info' })
}

//获取登录验证码
export const sendSmsCode = (data: SmsCodeVO) => {
  return request.post({ url: '/system/auth/send-sms-code', data })
}

// 短信验证码登录
export const smsLogin = (data: SmsLoginVO) => {
  return request.post({ url: '/system/auth/sms-login', data })
}

// 社交快捷登录，使用 code 授权码
export function socialLogin(type: string, code: string, state: string) {
  return request.post({
    url: '/system/auth/social-login',
    data: {
      type,
      code,
      state
    }
  })
}

// 社交授权的跳转
export const socialAuthRedirect = (type: number, redirectUri: string) => {
  return request.get({
    url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri
  })
}
// 获取验证图片以及 token
export const getCode = (data) => {
  return request.postOriginal({ url: 'system/captcha/get', data })
}

// 滑动或者点选验证
export const reqCheck = (data) => {
  return request.postOriginal({ url: 'system/captcha/check', data })
}

// 发送重置教师密码短信
export const sendResetPasswordSmsToUsername = (data: AuthResetPasswordReqVO) => {
  return request.post({ url: '/system/auth/send-reset-sms', data })
}

// 发送重置教师密码邮件
export const sendResetPasswordMailToUsername = (data: AuthResetPasswordReqVO) => {
  return request.post({ url: '/system/auth/send-reset-mail', data })
}

// 重置教师密码
export const resetPassword = (data: AuthResetPasswordSaveReqVO) => {
  return request.post({ url: '/system/auth/reset-pwd', data })
}
